﻿using System;
using System.Threading;
using System.Windows.Forms;
using Microsoft.CRM.ThirdParty.Extensions.ProjectCreator.Eventing;
using Microsoft.CRM.ThirdParty.Extensions.ProjectCreator.Presentation;

namespace Microsoft.CRM.ThirdParty.Extensions.ProjectCreator
{
    public partial class ProjectCreatorForm : Form, IProjectCreationFormView
    {
        private ProjectCreationFormPresenter _presenter;
        private ProjectEventAggregator _aggregator;
        private readonly SynchronizationContext _uiThread;

        public ProjectCreatorForm()
        {
            InitializeComponent();

            _aggregator = new ProjectEventAggregator();
            _presenter = new ProjectCreationFormPresenter(this);
            _uiThread = SynchronizationContext.Current;
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            _aggregator.Subscribe(this);
            _presenter.CreateProject();
        }

        public string ProjectFolder
        {
            get
            {
                var projlocation = String.Empty;

                _uiThread.Send(x => projlocation = txtProjectLocation.Text, null);

                return projlocation;
            }
        }

        public string ProjectName
        {
            get
            {

                var projname = String.Empty;

                _uiThread.Send(x => projname = txtProjectName.Text, null);

                return projname;
            }
        }

        public void AssignMessage(string message)
        {
            _uiThread.Post(x =>
                               {
                                   txtOutput.Text += message + Environment.NewLine;
                               },null);
        }

        public void ClearMessages()
        {
            _uiThread.Send(x =>
            {
                txtOutput.Text += String.Empty;
            }, null);
        }
    }
}
